package com.xiao.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiao.train.business.domain.*;
import com.xiao.train.business.req.SeatSellReq;
import com.xiao.train.business.resp.DailyTrainSeatQueryResp;
import com.xiao.train.business.resp.SeatSellResp;
import com.xiao.train.common.resp.PageResp;
import com.xiao.train.common.util.SnowUtil;
import com.xiao.train.business.mapper.DailyTrainSeatMapper;
import com.xiao.train.business.req.DailyTrainSeatQueryReq;
import com.xiao.train.business.req.DailyTrainSeatSaveReq;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DailyTrainSeatService {

    @Resource
    DailyTrainSeatMapper dailyTrainSeatMapper;
    
    @Resource
    TrainSeatService trainSeatService;

    @Resource
    TrainStationService trainStationService;

    public void save(DailyTrainSeatSaveReq req) {
        DateTime now = DateTime.now();
        DailyTrainSeat dailyTrainSeat = BeanUtil.copyProperties(req, DailyTrainSeat.class);
        if (ObjectUtil.isNull(dailyTrainSeat.getId())) {
            dailyTrainSeat.setId(SnowUtil.getId());
            dailyTrainSeat.setCreateTime(now);
            dailyTrainSeat.setUpdateTime(now);
            dailyTrainSeatMapper.insert(dailyTrainSeat);
        } else {
            dailyTrainSeat.setUpdateTime(now);
            dailyTrainSeatMapper.updateByPrimaryKey(dailyTrainSeat);
        }
    }
    public PageResp<DailyTrainSeatQueryResp> queryList(DailyTrainSeatQueryReq req) {
        DailyTrainSeatExample dailyTrainSeatExample = new DailyTrainSeatExample();
        DailyTrainSeatExample.Criteria criteria = dailyTrainSeatExample.createCriteria();
        dailyTrainSeatExample.setOrderByClause("train_code asc, carriage_index asc, carriage_seat_index asc");
        if (ObjectUtil.isNotEmpty(req.getTrainCode())) {
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }
        log.info("查询页码: {}", req.getPage());
        log.info("每页条数: {}", req.getSize());
        PageHelper.startPage(req.getPage(), req.getSize());
        List<DailyTrainSeat> dailyTrainSeats = dailyTrainSeatMapper.selectByExample(dailyTrainSeatExample);

        PageInfo<DailyTrainSeat> pageInfo = new PageInfo<>(dailyTrainSeats);
        log.info("总行数: {}", pageInfo.getTotal());
        log.info("总页数: {}", pageInfo.getPages());

        PageResp<DailyTrainSeatQueryResp> pageResp = new PageResp<>();
        List<DailyTrainSeatQueryResp> list = BeanUtil.copyToList(dailyTrainSeats, DailyTrainSeatQueryResp.class);
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }
    public void delete(Long id) {
        dailyTrainSeatMapper.deleteByPrimaryKey(id);
    }
    @Transactional
    public void genDaily(Date date, String trainCode) {
        log.info("生成 日期[{}] 车次[{}] 的车座信息开始", DateUtil.formatDate(date), trainCode);
        //删除符合条件的车座信息
        DailyTrainSeatExample dailyTrainSeatExample = new DailyTrainSeatExample();
        dailyTrainSeatExample.createCriteria().andTrainCodeEqualTo(trainCode).andDateEqualTo(date);
        dailyTrainSeatMapper.deleteByExample(dailyTrainSeatExample);
        //新增车座信息
        List<TrainSeat> trainSeats = trainSeatService.selectByTrainCode(trainCode);
        if (CollectionUtil.isEmpty(trainSeats)) {
            log.info("该车次 基础车座信息为空,生成该车次车座信息结束");
            return;
        }
        List<TrainStation> trainStations = trainStationService.selectByTrainCode(trainCode);
        String sell = StrUtil.fillBefore("", '0', trainStations.size() - 1);
        for (TrainSeat trainSeat : trainSeats) {
            DateTime now = DateTime.now();
            DailyTrainSeat dailyTrainSeat = BeanUtil.copyProperties(trainSeat, DailyTrainSeat.class);
            dailyTrainSeat.setId(SnowUtil.getId());
            dailyTrainSeat.setCreateTime(now);
            dailyTrainSeat.setUpdateTime(now);
            dailyTrainSeat.setDate(date);
            dailyTrainSeat.setSell(sell);
            dailyTrainSeatMapper.insert(dailyTrainSeat);
        }
        log.info("生成 日期[{}] 车次[{}] 的车座信息结束", DateUtil.formatDate(date), trainCode);
    }
    public int countSeat(Date date, String trainCode, String seatType) {
        DailyTrainSeatExample dailyTrainSeatExample = new DailyTrainSeatExample();
        dailyTrainSeatExample.createCriteria().andDateEqualTo(date).andTrainCodeEqualTo(trainCode)
                .andSeatTypeEqualTo(seatType);
        int count = (int) dailyTrainSeatMapper.countByExample(dailyTrainSeatExample);
        return count == 0 ? -1 : count;
    }
    public List<DailyTrainSeat> selectByCarriage(Date date, String trainCode,
                                                 Integer carriageSeatIndex) {
        DailyTrainSeatExample example = new DailyTrainSeatExample();
        example.setOrderByClause("carriage_seat_index asc");

        example.createCriteria().andTrainCodeEqualTo(trainCode).andDateEqualTo(date)
                .andCarriageIndexEqualTo(carriageSeatIndex);
        return dailyTrainSeatMapper.selectByExample(example);
    }

    /**
     * 查询某日某车次的所有座位
     */
    public List<SeatSellResp> querySeatSell(SeatSellReq req) {
        Date date = req.getDate();
        String trainCode = req.getTrainCode();
        log.info("查询日期【{}】车次【{}】的座位销售信息", DateUtil.formatDate(date), trainCode);
        DailyTrainSeatExample dailyTrainSeatExample = new DailyTrainSeatExample();
        dailyTrainSeatExample.setOrderByClause("`carriage_index` asc, carriage_seat_index asc");
        dailyTrainSeatExample.createCriteria()
                .andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode);
        return BeanUtil.copyToList(dailyTrainSeatMapper.selectByExample(dailyTrainSeatExample), SeatSellResp.class);
    }

}
